package com.xie.leetcode.array;

//5. 最长回文子串
//        给你一个字符串 s，找到 s 中最长的回文子串。
//
//
//
//        示例 1：
//
//        输入：s = "babad"
//        输出："bab"
//        解释："aba" 同样是符合题意的答案。
//        示例 2：
//
//        输入：s = "cbbd"
//        输出："bb"
//        示例 3：
//
//        输入：s = "a"
//        输出："a"
//        示例 4：
//
//        输入：s = "ac"
//        输出："a"
//
//
//        提示：
//
//        1 <= s.length <= 1000
//        s 仅由数字和英文字母（大写和/或小写）组成

/**
 * @author xiezhendong
 * @date 2021/10/28
 */
public class LongestPalindrome {

    public static void main(String[] args) {
        LongestPalindrome longestPalindrome = new LongestPalindrome();
        System.out.println(longestPalindrome.longestPalindrome("babab"));
        System.out.println(longestPalindrome.longestPalindrome("cbbd"));
        System.out.println(longestPalindrome.longestPalindrome("a"));
        System.out.println(longestPalindrome.longestPalindrome("ac"));
        System.out.println(longestPalindrome.longestPalindrome("ujtofmboiyyrjzbonysurqfxylvhuzzrzqwcjxibhawifptuammlxstcjmcmfvjuphyyfflkcbwimmpehqrqcdqxglqciduhhuhbjnwaaywofljhwzuqsnhyhahtkilwggineoosnqhdluahhkkbcwbupjcuvzlbzocgmkkyhhglqsvrxsgcglfisbzbawitbjwycareuhyxnbvounqdqdaixgqtljpxpyrccagrkdxsdtvgdjlifknczaacdwxropuxelvmcffiollbuekcfkxzdzuobkrgjedueyospuiuwyppgiwhemyhdjhadcabhgtkotqyneioqzbxviebbvqavtvwgyyrjhnlceyedhfechrbhugotqxkndwxukwtnfiqmstaadlsebfopixrkbvetaoycicsdndmztyqnaehnozchrakt"));
        System.out.println(longestPalindrome.longestPalindrome("gphyvqruxjmwhonjjrgumxjhfyupajxbjgthzdvrdqmdouuukeaxhjumkmmhdglqrrohydrmbvtuwstgkobyzjjtdtjroqpyusfsbjlusekghtfbdctvgmqzeybnwzlhdnhwzptgkzmujfldoiejmvxnorvbiubfflygrkedyirienybosqzrkbpcfidvkkafftgzwrcitqizelhfsruwmtrgaocjcyxdkovtdennrkmxwpdsxpxuarhgusizmwakrmhdwcgvfljhzcskclgrvvbrkesojyhofwqiwhiupujmkcvlywjtmbncurxxmpdskupyvvweuhbsnanzfioirecfxvmgcpwrpmbhmkdtckhvbxnsbcifhqwjjczfokovpqyjmbywtpaqcfjowxnmtirdsfeujyogbzjnjcmqyzciwjqxxgrxblvqbutqittroqadqlsdzihngpfpjovbkpeveidjpfjktavvwurqrgqdomiibfgqxwybcyovysydxyyymmiuwovnevzsjisdwgkcbsookbarezbhnwyqthcvzyodbcwjptvigcphawzxouixhbpezzirbhvomqhxkfdbokblqmrhhioyqubpyqhjrnwhjxsrodtblqxkhezubprqftrqcyrzwywqrgockioqdmzuqjkpmsyohtlcnesbgzqhkalwixfcgyeqdzhnnlzawrdgskurcxfbekbspupbduxqxjeczpmdvssikbivjhinaopbabrmvscthvoqqbkgekcgyrelxkwoawpbrcbszelnxlyikbulgmlwyffurimlfxurjsbzgddxbgqpcdsuutfiivjbyqzhprdqhahpgenjkbiukurvdwapuewrbehczrtswubthodv"));
    }

    public String longestPalindrome(String s) {
        if (s == null || "".equals(s)) {
            return s;
        }
        char[] chars = s.toCharArray();
        if (chars.length == 1) {
            return s;
        }

        int resLen = 0;
        int len = s.length();
        int start = 0;
        for (int i = 0; i < 2 * len - 1; i++) {
            int left = i / 2;
            int right = (i + 1) / 2;
            while (left >= 0 && right < len && chars[left] == chars[right]) {
                if (right - left + 1 > resLen) {
                    start = left;
                    resLen = right - left + 1;
                }
                left--;
                right++;
            }
        }
        return s.substring(start, start + resLen);
    }

    /**
     * 暴力破解
     *
     * @param s
     * @return
     */
    public String longestPalindrome1(String s) {
        if (s == null || "".equals(s)) {
            return s;
        }
        char[] chars = s.toCharArray();
        if (chars.length == 1) {
            return s;
        }
        for (int i = chars.length; i > 0; i--) {
            int j = 0;
            while (j + i <= chars.length) {
                String sValue = s.substring(j, j + i);
                if (check(sValue)) {
                    return sValue;
                }
                j++;
            }
        }
        return "";
    }

    private boolean check(String sValue) {
        char[] chars = sValue.toCharArray();
        int num = chars.length / 2;
        for (int i = 0; i < num; i++) {
            if (chars[i] != chars[chars.length - 1 - i]) {
                return false;
            }
        }
        return true;
    }
}
